#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.

from esr_parser import world_pc, pc, world_mobile, mobile
import json
import os

def get_world_pc_ranking(keyword, order_by=world_pc.Order.general, tm_only=False, limit=100, output_dir=None):
    if isinstance(keyword, str):
        keyword = keyword.decode('utf-8')

    if not output_dir:
        output_dir = u"world_pc_{}__output".format(keyword)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    task = world_pc.Rank_Task(keyword, order_by=world_pc.Order.general, tm_only=tm_only, limit=limit)
    result_items, result_right_items, result_bottom_items = task.process()

    with open(os.path.join(output_dir, 'items'), 'w+') as f:
        for item in result_items:
            f.write(json.dumps(item) + "\n")

    with open(os.path.join(output_dir, 'right_items'), 'w+') as f:
        for item in result_right_items:
            f.write(json.dumps(item) + "\n")

    with open(os.path.join(output_dir, 'bottom_items'), 'w+') as f:
        for item in result_bottom_items:
            f.write(json.dumps(item) + "\n")



def get_pc_ranking(keyword, order_by=pc.Order.general, tm_only=False, limit=100, output_dir=None):
    if isinstance(keyword, str):
        keyword = keyword.decode('utf-8')

    if not output_dir:
        output_dir = u"pc_{}__output".format(keyword)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    task = pc.Rank_Task(keyword, order_by=pc.Order.general, tm_only=tm_only, limit=limit)
    result_items, result_right_items, result_bottom_items = task.process()

    with open(os.path.join(output_dir, 'items'), 'w+') as f:
        for item in result_items:
            f.write(json.dumps(item) + "\n")

    with open(os.path.join(output_dir, 'right_items'), 'w+') as f:
        for item in result_right_items:
            f.write(json.dumps(item) + "\n")

    with open(os.path.join(output_dir, 'bottom_items'), 'w+') as f:
        for item in result_bottom_items:
            f.write(json.dumps(item) + "\n")


def get_world_mobile_ranking(keyword, order_by=world_mobile.Order.general, tm_only=False, limit=50, output_dir=None):
    if isinstance(keyword, str):
        keyword = keyword.decode('utf-8')

    if not output_dir:
        output_dir = u"world_mobile_{}__output".format(keyword)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    task = world_mobile.Rank_Task(keyword, order_by=world_mobile.Order.general, tm_only=False, limit=limit)
    result_items = task.process()

    with open(os.path.join(output_dir, 'items'), 'w+') as f:
        for item in result_items:
            f.write(json.dumps(item) + "\n")

def get_mobile_ranking(keyword, order_by=mobile.Order.general, tm_only=False, limit=50, output_dir=None):
    if isinstance(keyword, str):
        keyword = keyword.decode('utf-8')

    if not output_dir:
        output_dir = u"mobile_{}__output".format(keyword)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    task = mobile.Rank_Task(keyword, order_by=mobile.Order.general, tm_only=tm_only, limit=limit)
    result_items = task.process()

    with open(os.path.join(output_dir, 'items'), 'w+') as f:
        for item in result_items:
            f.write(json.dumps(item) + "\n")


def item_detail(item_id):
    detail = detail.download(item_id)
    print detail
